package com.yanqu.road.dao.impl.servercenter;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.servercenter.HotfixUpdateInfo;
import com.yanqu.road.entity.servercenter.HotfixVersion;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 热更包版本信息
 */
public class HotfixVersionDaoImpl extends TempDao {

	public Map<String, Map<String, Map<String, List<HotfixVersion>>>> getHotfixVersionMap() {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_s_hotfix_version where isDelete=0";
		Map<String, Map<String, Map<String, List<HotfixVersion>>>> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				HotfixVersion data = new HotfixVersion();
				data.setPackageMark(rs.getString("packageMark"));
				data.setPackageVersion(rs.getString("packageVersion"));
				data.setHotfixVersion(rs.getString("hotfixVersion"));
				data.setUpdateScopeStr(rs.getString("updateScope"));
				data.setReview(rs.getBoolean("isReview"));
				data.setUpdateLevel(rs.getInt("updateLevel"));
				data.setUpdateVersion(rs.getString("updateVersion"));
				data.setReviewAppIdList(StringUtils.stringToStringList(rs.getString("reviewAppIdList"), ";"));
				if(!dataMap.containsKey(data.getPackageMark())){
					dataMap.put(data.getPackageMark(), new ConcurrentHashMap<>());
				}
				Map<String, Map<String, List<HotfixVersion>>> tempMap = dataMap.get(data.getPackageMark());
				if(!tempMap.containsKey(data.getPackageVersion())){
					tempMap.put(data.getPackageVersion(), new ConcurrentHashMap<>());
				}
				Map<String, List<HotfixVersion>> verMap = tempMap.get(data.getPackageVersion());
				if(!verMap.containsKey(data.getHotfixVersion())){
					verMap.put(data.getHotfixVersion(), new ArrayList<>());
				}
				List<HotfixVersion> dataList = verMap.get(data.getHotfixVersion());
				dataList.add(data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public Map<String, HotfixUpdateInfo> getHotfixUpdateInfoMap() {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_s_hotfix_update";
		Map<String, HotfixUpdateInfo> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				HotfixUpdateInfo data = new HotfixUpdateInfo();
				data.setPackageMark(rs.getString("packageMark"));
				data.setUpdateLevel(rs.getInt("updateLevel"));
				data.setUpdateVersion(rs.getString("updateVersion"));
				data.setWhiteUpdateLevel(rs.getInt("whiteUpdateLevel"));
				data.setWhiteUpdateVersion(rs.getString("whiteUpdateVersion"));
				data.setForwardUpdateLevel(rs.getInt("forwardUpdateLevel"));
				data.setForwardUpdateVersion(rs.getString("forwardUpdateVersion"));
				dataMap.put(data.getPackageMark(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}
}
